package com.cqeec.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import com.cqeec.action.BookAction;
import com.cqeec.bean.BookInfo;
import com.cqeec.util.ValidateUtil;

/**
 * 
 * @author HuaL
 * @description 图书信息主窗体
 */
public class BookFrame {
	static JInternalFrame bookFrame = null;
	// 工具条
	static JToolBar toolBar = new JToolBar();
	static JButton addBtn = new JButton();
	static JButton delBtn = new JButton();
	static JButton modBtn = new JButton();
	static JButton saveBtn = new JButton();
	static JButton findBtn = new JButton();
	static JButton returnBtn = new JButton();
	// 状态栏
	static JToolBar stateBar = new JToolBar();
	static JLabel lbState = new JLabel("状态");
	// 面板
	static JPanel mainPanl = new JPanel();
	static BookListPanel listPanl = null;
	static BookFindPanel findPanl = null;
	static BookEditPanel editPanl = null;
	static String flag = "";
	static BookInfo info = new BookInfo();
	static BookAction action = new BookAction();

	public BookFrame() {
	}

	public static void show(JDesktopPane deskPane) {
		if (bookFrame == null) {
			bookFrame = new JInternalFrame("图书信息", false, true, false, true);
			bookFrame.setSize(900, 600);
			deskPane.add(bookFrame);
			final Container c = bookFrame.getContentPane();
			Init(c);
			bookFrame.setVisible(true);
		}
		bookFrame.addInternalFrameListener(new InternalFrameAdapter() {
			public void internalFrameClosing(InternalFrameEvent e) {
				//关闭窗体前释放对象
				flag = "";
				lbState = null;
				listPanl = null;
				findPanl = null;
				editPanl = null;
				mainPanl = null;
				toolBar = null;
				addBtn = null;
				delBtn = null;
				modBtn = null;
				saveBtn = null;
				findBtn = null;
				returnBtn = null;
				stateBar = null;
				lbState = null;
				info = null;
				action = null;
				bookFrame = null;
			}

		});
	}

	public static void Init(Container c) {
		lbState = new JLabel("状态");
		info = new BookInfo();
		action = new BookAction();
		mainPanl = new JPanel();
		toolBar = new JToolBar();
		addBtn = new JButton();
		addBtn.setToolTipText("添加");
		addBtn.setIcon(new ImageIcon("ico\\new.jpg"));
		addBtn.addActionListener(new BookEventActionListener());
		toolBar.add(addBtn);
		
		delBtn = new JButton();
		delBtn.setToolTipText("删除");
		delBtn.setIcon(new ImageIcon("ico\\del.jpg"));
		delBtn.addActionListener(new BookEventActionListener());
		toolBar.add(delBtn);

		modBtn = new JButton();
		modBtn.setToolTipText("修改");
		modBtn.setIcon(new ImageIcon("ico\\update.jpg"));
		modBtn.addActionListener(new BookEventActionListener());
		toolBar.add(modBtn);

		saveBtn = new JButton();
		saveBtn.setToolTipText("保存");
		saveBtn.setIcon(new ImageIcon("ico\\save.jpg"));
		saveBtn.addActionListener(new BookEventActionListener());
		toolBar.add(saveBtn);

		findBtn = new JButton();
		findBtn.setToolTipText("查询");
		findBtn.setIcon(new ImageIcon("ico\\find.jpg"));
		findBtn.addActionListener(new BookEventActionListener());
		toolBar.add(findBtn);

		returnBtn = new JButton();
		returnBtn.setToolTipText("返回");
		returnBtn.setIcon(new ImageIcon("ico\\return.jpg"));
		returnBtn.addActionListener(new BookEventActionListener());
		toolBar.add(returnBtn);
		c.setLayout(new BorderLayout());
		toolBar.setFloatable(false);
		c.add(toolBar, BorderLayout.NORTH);
		// 状态栏
		stateBar = new JToolBar();
		stateBar.add(lbState);
		stateBar.setFloatable(false);
		c.add(stateBar, BorderLayout.SOUTH);
		mainPanl.setLayout(new BorderLayout());

		listPanl = new BookListPanel();
		listPanl.getBookList(info);
		mainPanl.add(listPanl, BorderLayout.CENTER);
		c.add(mainPanl, BorderLayout.CENTER);
	}
}

class BookEventActionListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == BookFrame.findBtn) {
			find();
		}
		if (e.getSource() == BookFrame.returnBtn) {
			ret();
		}
		if (e.getSource() == BookFrame.addBtn) {
			add();
		}
		if (e.getSource() == BookFrame.modBtn) {
			edit();
		}
		if (e.getSource() == BookFrame.saveBtn) {
			save();
		}
		if (e.getSource() == BookFrame.delBtn) {
			del();
		}
	}

	private void add() {
		BookFrame.editPanl = new BookEditPanel("add");
		BookFrame.mainPanl.removeAll();
		BookFrame.mainPanl.add(BookFrame.editPanl, BorderLayout.CENTER);
	}

	private void edit() {
		if (BookFrame.listPanl.table != null){
			int row = BookFrame.listPanl.table.getSelectedRow();
			if (row >= 0) {
				BookFrame.editPanl = new BookEditPanel("edit");
				BookFrame.editPanl.txtIsbn.setText(BookFrame.listPanl.table
						.getValueAt(row, 0).toString());
				BookFrame.editPanl.txtName.setText(BookFrame.listPanl.table
						.getValueAt(row, 1).toString());
				BookFrame.editPanl.xbType.setSelectedItem(BookFrame.listPanl.table
						.getValueAt(row, 2).toString());
				BookFrame.editPanl.txtstocknum.setText(BookFrame.listPanl.table
						.getValueAt(row, 3).toString());
				BookFrame.editPanl.txtAuthor.setText(BookFrame.listPanl.table
						.getValueAt(row, 4).toString());
				BookFrame.editPanl.txtPublishDate.setText(BookFrame.listPanl.table
						.getValueAt(row, 5).toString());
				BookFrame.editPanl.txtPublisher.setText(BookFrame.listPanl.table
						.getValueAt(row, 6).toString());
				BookFrame.editPanl.txtPrice.setText(BookFrame.listPanl.table
						.getValueAt(row, 7).toString());
				BookFrame.mainPanl.removeAll();
				BookFrame.mainPanl.add(BookFrame.editPanl, BorderLayout.CENTER);
			} else {
				JOptionPane.showMessageDialog(null, "请选择要修改的数据行", "图书信息",JOptionPane.WARNING_MESSAGE);
			}
		}else{
			JOptionPane.showMessageDialog(null, "没有需要修改的数据行", "图书信息",JOptionPane.WARNING_MESSAGE);
		}
		
	}

	private void save() {
		if (BookFrame.editPanl != null) {
			BookInfo info = new BookInfo();
			info.setBookid(BookFrame.editPanl.txtIsbn.getText().trim());
			info.setBookname(BookFrame.editPanl.txtName.getText().trim());
			info.setBooktype(BookFrame.editPanl.xbType.getSelectedItem()
					.toString());
			info.setAuthor(BookFrame.editPanl.txtAuthor.getText().trim());
			info.setPublisher(BookFrame.editPanl.txtPublisher.getText().trim());
			info.setPublishdate(BookFrame.editPanl.txtPublishDate.getText()
					.trim());
			info.setPrice(BookFrame.editPanl.txtPrice.getText().trim());
			info.setStocknum(BookFrame.editPanl.txtstocknum.getText().trim());

			// 判断数据是否填写完整以及数据类型是否正确
			if (info.getBookid().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请填写图书编号", "图书信息",JOptionPane.WARNING_MESSAGE);
				BookFrame.editPanl.txtIsbn.requestFocusInWindow();
				return;
			}
			if (info.getBookid().trim().length() != 13) {
				JOptionPane.showMessageDialog(null, "图书编号长度13位", "图书信息",JOptionPane.ERROR_MESSAGE);
				BookFrame.editPanl.txtIsbn.requestFocusInWindow();
				return;
			}
			if (info.getBookname().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请填写图书名称", "图书信息",JOptionPane.WARNING_MESSAGE);
				BookFrame.editPanl.txtName.requestFocusInWindow();
				return;
			}
			if (info.getBookname().trim().length() > 25) {
				JOptionPane.showMessageDialog(null, "图书名称长度不能超出25", "图书信息",JOptionPane.ERROR_MESSAGE);
				BookFrame.editPanl.txtName.requestFocusInWindow();
				return;
			}
			if (info.getAuthor().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请填写图书作者", "图书信息",JOptionPane.WARNING_MESSAGE);
				BookFrame.editPanl.txtAuthor.requestFocusInWindow();
				return;
			}
			if (info.getAuthor().trim().length() > 25) {
				JOptionPane.showMessageDialog(null, "图书作者长度不能超出25", "图书信息",JOptionPane.ERROR_MESSAGE);
				BookFrame.editPanl.txtAuthor.requestFocusInWindow();
				return;
			}
			if (info.getPublisher().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请填写出版社", "图书信息",JOptionPane.WARNING_MESSAGE);
				BookFrame.editPanl.txtPublisher.requestFocusInWindow();
				return;
			}
			if (info.getPublisher().trim().length() > 25) {
				JOptionPane.showMessageDialog(null, "出版社名称长度不能超出25", "图书信息",JOptionPane.ERROR_MESSAGE);
				BookFrame.editPanl.txtPublisher.requestFocusInWindow();
				return;
			}
			if (info.getPublishdate().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请填写出版日期", "图书信息",JOptionPane.WARNING_MESSAGE);
				BookFrame.editPanl.txtPublishDate.requestFocusInWindow();
				return;
			}
			if (!ValidateUtil.StringToDate(info.getPublishdate().trim())) {// 验证数据类型
				JOptionPane.showMessageDialog(null, "输入的日期格式有误，请按yyyy-MM-dd格式输入", "图书信息",JOptionPane.ERROR_MESSAGE);
				BookFrame.editPanl.txtPublishDate.requestFocusInWindow();
				return;
			}
			if (info.getPrice().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请填写图书价格", "图书信息",JOptionPane.WARNING_MESSAGE);
				BookFrame.editPanl.txtPrice.requestFocusInWindow();
				return;
			}
			if (ValidateUtil.StringTodouble(info.getPrice().trim()).equals(
					"err")) {// 验证数据类型
				JOptionPane.showMessageDialog(null, "输入的单价格式有误，请重新输入", "图书信息",JOptionPane.ERROR_MESSAGE);
				BookFrame.editPanl.txtPrice.requestFocusInWindow();
				return;
			} else {
				if (Double.parseDouble(info.getPrice().trim()) <= 0) {
					JOptionPane.showMessageDialog(null, "图书单价必须大于零，请重新输入", "图书信息",JOptionPane.ERROR_MESSAGE);
					BookFrame.editPanl.txtPrice.requestFocusInWindow();
					return;
				} else {
					info.setPrice(ValidateUtil.StringTodouble(info.getPrice()
							.trim()));
				}
			}
			if (info.getStocknum().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请填写图书数量", "图书信息",JOptionPane.WARNING_MESSAGE);
				BookFrame.editPanl.txtstocknum.requestFocusInWindow();
				return;
			}
			if (!ValidateUtil.StringToInt(info.getStocknum().trim())) {// 验证数据类型
				JOptionPane.showMessageDialog(null, "输入的数量格式有误，请重新输入", "图书信息",JOptionPane.ERROR_MESSAGE);
				BookFrame.editPanl.txtstocknum.requestFocusInWindow();
				return;
			} else {
				if (Integer.parseInt(info.getStocknum().trim()) <= 0) {
					JOptionPane.showMessageDialog(null, "图书数量必须大于零，请重新输入", "图书信息",JOptionPane.ERROR_MESSAGE);
					BookFrame.editPanl.txtstocknum.requestFocusInWindow();
					return;
				}
			}
			
			boolean reValue = false;
			if (BookFrame.editPanl.flag.equals("edit")) {
				reValue = BookFrame.action.update(info);
			} else {
				if (!BookFrame.action.beforeadd(info.getBookid().trim())){
					JOptionPane.showMessageDialog(null, "图书已添加过，请检查数据后重新录入图书编号", "图书信息",JOptionPane.ERROR_MESSAGE);
					BookFrame.editPanl.txtIsbn.requestFocusInWindow();
					return;
				}else{
					reValue = BookFrame.action.add(info);
				}
			}
			if (reValue) {
				JOptionPane.showMessageDialog(null, "操作成功", "图书信息",JOptionPane.INFORMATION_MESSAGE);
				ret();
			} else {
				JOptionPane.showMessageDialog(null, "操作失败，请与系统管理员联系", "图书信息",JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	private void del() {
		if (BookFrame.listPanl.table != null){
			int row = BookFrame.listPanl.table.getSelectedRow();
			if (row >= 0) {
				int i = JOptionPane.showConfirmDialog(null, "是否确认删除该行数据？", "确认",
						JOptionPane.YES_NO_OPTION);
				if (i == 1) {
					return;
				}
				String bookid = BookFrame.listPanl.table.getValueAt(row, 0)
						.toString();
				if (BookFrame.action.beforedel(bookid)){
					if (BookFrame.action.del(bookid)) {
						JOptionPane.showMessageDialog(null, "操作成功", "图书信息",JOptionPane.INFORMATION_MESSAGE);
						ret();
					} else {
						JOptionPane.showMessageDialog(null, "操作失败，请与系统管理员联系", "图书信息",JOptionPane.ERROR_MESSAGE);
					}
				}else{
					JOptionPane.showMessageDialog(null, "当前图书有借阅无法删除", "图书信息",JOptionPane.ERROR_MESSAGE);
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择要删除的数据行", "图书信息",JOptionPane.WARNING_MESSAGE);
			}
		}else{
			JOptionPane.showMessageDialog(null, "没有需要删除的数据行", "图书信息",JOptionPane.WARNING_MESSAGE);
		}
		
	}

	private void ret() {
		BookFrame.flag = "";
		BookFrame.listPanl = new BookListPanel();
		BookFrame.listPanl.getBookList(BookFrame.info);
		BookFrame.mainPanl.removeAll();
		BookFrame.mainPanl.add(BookFrame.listPanl, BorderLayout.CENTER);
	}

	private void find() {
		if (BookFrame.flag.equals("query")) {
			if (BookFrame.findPanl.cbPublishDate.isSelected()) {
				if (!BookFrame.findPanl.txtStartDate.getText().trim()
						.equals("")) {
					if (!ValidateUtil
							.StringToDate(BookFrame.findPanl.txtStartDate
									.getText().trim())) {// 验证数据类型
						JOptionPane.showMessageDialog(null, "输入的日期格式有误，请按yyyy-MM-dd格式输入", "图书信息",JOptionPane.ERROR_MESSAGE);
						BookFrame.findPanl.txtStartDate.requestFocusInWindow();
						return;
					}
				}
				BookFrame.info.setStartDate(BookFrame.findPanl.txtStartDate
						.getText().trim());
				if (!BookFrame.findPanl.txtEndDate.getText().trim().equals("")) {
					if (!ValidateUtil
							.StringToDate(BookFrame.findPanl.txtEndDate
									.getText().trim())) {// 验证数据类型
						JOptionPane.showMessageDialog(null, "输入的日期格式有误，请按yyyy-MM-dd格式输入", "图书信息",JOptionPane.ERROR_MESSAGE);
						BookFrame.findPanl.txtEndDate.requestFocusInWindow();
						return;
					}
				}
				BookFrame.info.setEndDate(BookFrame.findPanl.txtEndDate
						.getText().trim());
			} else {
				BookFrame.info.setStartDate("");
				BookFrame.info.setEndDate("");
			}
			if (BookFrame.findPanl.cbPrice.isSelected()) {
				if (!BookFrame.findPanl.txtMinPrice.getText().trim().equals("")) {
					if (ValidateUtil.StringTodouble(
							BookFrame.findPanl.txtMinPrice.getText().trim())
							.equals("err")) {// 验证数据类型
						JOptionPane.showMessageDialog(null, "输入的单价格式有误，请重新输入", "图书信息",JOptionPane.ERROR_MESSAGE);
						BookFrame.findPanl.txtMinPrice.requestFocusInWindow();
						return;
					}
				}
				BookFrame.info.setMinPrice(ValidateUtil
						.StringTodouble(BookFrame.findPanl.txtMinPrice
								.getText().trim()));
				if (!BookFrame.findPanl.txtMaxPrice.getText().trim().equals("")) {
					if (ValidateUtil.StringTodouble(
							BookFrame.findPanl.txtMaxPrice.getText().trim())
							.equals("err")) {// 验证数据类型
						JOptionPane.showMessageDialog(null, "输入的单价格式有误，请重新输入", "图书信息",JOptionPane.ERROR_MESSAGE);
						BookFrame.findPanl.txtMaxPrice.requestFocusInWindow();
						return;
					}
				}
				BookFrame.info.setMaxPrice(ValidateUtil
						.StringTodouble(BookFrame.findPanl.txtMaxPrice
								.getText().trim()));
			} else {
				BookFrame.info.setMinPrice("");
				BookFrame.info.setMaxPrice("");
			}

			if (BookFrame.findPanl.cbIsbn.isSelected()) {
				BookFrame.info.setBookid(BookFrame.findPanl.txtIsbn.getText()
						.trim());
			} else {
				BookFrame.info.setBookid("");
			}
			if (BookFrame.findPanl.cbName.isSelected()) {
				BookFrame.info.setBookname(BookFrame.findPanl.txtName.getText()
						.trim());
			} else {
				BookFrame.info.setBookname("");
			}
			if (BookFrame.findPanl.cbType.isSelected()) {
				BookFrame.info.setBooktype(BookFrame.findPanl.Type
						.getSelectedItem().toString().trim());
			} else {
				BookFrame.info.setBooktype("");
			}
			if (BookFrame.findPanl.cbAuthor.isSelected()) {
				BookFrame.info.setAuthor(BookFrame.findPanl.txtAuthor.getText()
						.trim());
			} else {
				BookFrame.info.setAuthor("");
			}
			if (BookFrame.findPanl.cbPublisher.isSelected()) {
				BookFrame.info.setPublisher(BookFrame.findPanl.txtPublisher
						.getText().trim());
			} else {
				BookFrame.info.setPublisher("");
			}
			ret();
			BookFrame.flag = "";
			return;
		}
		BookFrame.findPanl = new BookFindPanel();
		BookFrame.mainPanl.removeAll();
		BookFrame.mainPanl.add(BookFrame.findPanl, BorderLayout.CENTER);
		BookFrame.flag = "query";
	}
}